#include "scheduleitem.h"
#include "ui_scheduleitem.h"
#include <QDateTime>
#include <QPainter>

ScheduleItem::ScheduleItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ScheduleItem)
{
    ui->setupUi(this);
    ui->detailwidget->hide();
    this->setMinimumHeight(140);
    this->setMaximumHeight(140);

    ui->p1_device_lb->setText("");
    ui->p2_device_lb->setText("");
    ui->p3_device_lb->setText("");
    ui->p4_device_lb->setText("");
    this->setAttribute(Qt::WA_DeleteOnClose, true);
}

ScheduleItem::~ScheduleItem()
{
    delete ui;
}

void ScheduleItem::setData(ScheduleInfo::schedule_t d)
{
    m_d = d;
    if (d.type.compare("互动") == 0) {
        ui->c1_type_ico_label->setStyleSheet("border-image: url(:/img/type1-normal.png);");
    } else {
        ui->c1_type_ico_label->setStyleSheet("border-image: url(:/img/type2-normal.png);");
    }
    ui->c1_type_label->setText(d.type);

    ui->c1_name_label->setText(d.name);

    QString teacher = d.lecturer;
    if (d.lecturer.count() > 4)
    {
        teacher = d.lecturer.left(4)+"...";
    }

    ui->teacher_lb->setText(teacher);
    ui->c1_time_label->setText(QDateTime::fromString(d.start_time,"yyyy-MM-dd hh:mm:ss").toString("hh:mm")
                               +"-"+QDateTime::fromString(d.end_time, "yyyy-MM-dd hh:mm:ss").toString("hh:mm"));

    QDateTime cur = QDateTime::currentDateTime();
    QString curStr = cur.toString("yyyy-MM-dd hh:mm:ss");
    if (curStr > d.start_time && curStr < d.end_time)
    {
        ui->conf_num_lb->setText(d.conference_number);
    }
    else
    {
        ui->conf_num_lb->setText("");
    }

    ui->lecturer_device_lb->setText(d.lecturer_device);

    if (d.participants.count() > 0)
    {
        QStringList slist = d.participants[0].split("*");
        if (slist.count() > 0)
        {
            ui->p1_device_lb->setText(slist[0]);
        }
//        if (slist.count() > 1)
//        {
//            ui->p1_name_lb->setText(slist[1]);
//        }
    }
    if (d.participants.count() > 1)
    {
        QStringList slist = d.participants[1].split("*");
        if (slist.count() > 0)
        {
            ui->p2_device_lb->setText(slist[0]);
        }

//        if (slist.count() > 1)
//        {
//            ui->p2_name_lb->setText(slist[1]);
//        }
    }

    if (d.participants.count() > 2)
    {
        QStringList slist = d.participants[2].split("*");
        if (slist.count() > 0)
        {
            ui->p3_device_lb->setText(slist[0]);
        }

//        if (slist.count() > 1)
//        {
//            ui->p2_name_lb->setText(slist[1]);
//        }
    }

    if (d.participants.count() > 3)
    {
        QStringList slist = d.participants[3].split("*");
        if (slist.count() > 0)
        {
            ui->p4_device_lb->setText(slist[0]);
        }

//        if (slist.count() > 1)
//        {
//            ui->p2_name_lb->setText(slist[1]);
//        }
    }
}

QString ScheduleItem::conferenceNumber()
{
    return ui->conf_num_lb->text();
}

QString ScheduleItem::starttime()
{
    return m_d.start_time;
}

QString ScheduleItem::endtime()
{
    return m_d.end_time;
}

void ScheduleItem::setLabelFocusStyle(bool t)
{
    if (t)
    {
        if (ui->c1_type_label->text().compare("互动") == 0)
        {
            ui->c1_type_ico_label->setStyleSheet("border-image:url(:/img/type1-focus.png);");
        }
        else
        {
            ui->c1_type_ico_label->setStyleSheet("border-image:url(:/img/type2-focus.png);");
        }

        ui->c1_type_ico_label_2->setProperty("selffocus","1");
        ui->c1_type_label->setProperty("selffocus","1");
        ui->c1_name_label->setProperty("selffocus","1");
        ui->teacher_lb->setProperty("selffocus","1");
        ui->c1_time_label->setProperty("selffocus","1");
        ui->conf_num_lb->setProperty("selffocus","1");
        ui->lecturer_device_lb->setProperty("selffocus","1");
        ui->p1_device_lb->setProperty("selffocus","1");
        ui->p2_device_lb->setProperty("selffocus","1");
        ui->p3_device_lb->setProperty("selffocus","1");
        ui->p4_device_lb->setProperty("selffocus","1");
        ui->c1_time_label_2->setProperty("selffocus","1");
        ui->c1_time_label_4->setProperty("selffocus","1");
        ui->c1_time_label_7->setProperty("selffocus","1");
        ui->c1_time_label_11->setProperty("selffocus","1");
    }
    else
    {
        if (ui->c1_type_label->text().compare("互动") == 0)
        {
            ui->c1_type_ico_label->setStyleSheet("border-image:url(:/img/type1-normal.png);");
        }
        else
        {
            ui->c1_type_ico_label->setStyleSheet("border-image:url(:/img/type2-normal.png);");
        }
        ui->c1_type_ico_label_2->setProperty("selffocus","0");
        ui->c1_type_label->setProperty("selffocus","0");
        ui->c1_name_label->setProperty("selffocus","0");
        ui->teacher_lb->setProperty("selffocus","0");
        ui->c1_time_label->setProperty("selffocus","0");
        ui->conf_num_lb->setProperty("selffocus","0");
        ui->lecturer_device_lb->setProperty("selffocus","0");
        ui->p1_device_lb->setProperty("selffocus","0");
        ui->p2_device_lb->setProperty("selffocus","0");
        ui->p3_device_lb->setProperty("selffocus","0");
        ui->p4_device_lb->setProperty("selffocus","0");
        ui->c1_time_label_2->setProperty("selffocus","0");
        ui->c1_time_label_4->setProperty("selffocus","0");
        ui->c1_time_label_7->setProperty("selffocus","0");
        ui->c1_time_label_11->setProperty("selffocus","0");
    }

    ui->c1_type_ico_label_2->style()->unpolish(ui->c1_type_ico_label_2);
    ui->c1_type_ico_label_2->style()->polish(ui->c1_type_ico_label_2);
    ui->c1_type_label->style()->unpolish(ui->c1_type_label);
    ui->c1_type_label->style()->polish(ui->c1_type_label);

    ui->c1_name_label->style()->unpolish(ui->c1_name_label);
    ui->c1_name_label->style()->polish(ui->c1_name_label);

    ui->teacher_lb->style()->unpolish(ui->teacher_lb);
    ui->teacher_lb->style()->polish(ui->teacher_lb);

    ui->c1_time_label->style()->unpolish(ui->c1_time_label);
    ui->c1_time_label->style()->polish(ui->c1_time_label);

    ui->conf_num_lb->style()->unpolish(ui->conf_num_lb);
    ui->conf_num_lb->style()->polish(ui->conf_num_lb);

    ui->lecturer_device_lb->style()->unpolish(ui->lecturer_device_lb);
    ui->lecturer_device_lb->style()->polish(ui->lecturer_device_lb);

    ui->p1_device_lb->style()->unpolish(ui->p1_device_lb);
    ui->p1_device_lb->style()->polish(ui->p1_device_lb);

    ui->p3_device_lb->style()->unpolish(ui->p3_device_lb);
    ui->p3_device_lb->style()->polish(ui->p3_device_lb);

    ui->p2_device_lb->style()->unpolish(ui->p2_device_lb);
    ui->p2_device_lb->style()->polish(ui->p2_device_lb);

    ui->p4_device_lb->style()->unpolish(ui->p4_device_lb);
    ui->p4_device_lb->style()->polish(ui->p4_device_lb);
    ui->c1_time_label_2->style()->unpolish(ui->c1_time_label_2);
    ui->c1_time_label_2->style()->polish(ui->c1_time_label_2);
    ui->c1_time_label_4->style()->unpolish(ui->c1_time_label_4);
    ui->c1_time_label_4->style()->polish(ui->c1_time_label_4);
    ui->c1_time_label_7->style()->unpolish(ui->c1_time_label_7);
    ui->c1_time_label_7->style()->polish(ui->c1_time_label_7);
    ui->c1_time_label_11->style()->unpolish(ui->c1_time_label_11);
    ui->c1_time_label_11->style()->polish(ui->c1_time_label_11);

}

void ScheduleItem::focusInEvent(QFocusEvent *e)
{
    emit hasFocus(true);
    setLabelFocusStyle(true);
    ui->detailwidget->show();
    this->setMinimumHeight(280);
    this->setMaximumHeight(280);
    QWidget::focusInEvent(e);
}

void ScheduleItem::focusOutEvent(QFocusEvent *e)
{
    emit hasFocus(false);
    setLabelFocusStyle(false);
    ui->detailwidget->hide();
    this->setMinimumHeight(140);
    this->setMaximumHeight(140);
    QWidget::focusOutEvent(e);
}


void ScheduleItem::paintEvent(QPaintEvent *e)
{
    Q_UNUSED(e);
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);//绘制样式
}
